
(function () {
 
    var url = new URL(window.location.protocol + document.currentScript.getAttribute("src"));
    var search =  new URLSearchParams(url.search);
    var debug = search.get('debug'); 

        //获取AccessToken 
        var accesstoken = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1, window.location.pathname.lastIndexOf('.'));
        //console.log(accesstoken);
        if (!accesstoken) {
            alert('无效的分享');
            return;
        }

    //入口
     window.main = function () { 
        var app = this; 
        app.checkSessionServiceName = "account.checksession";
        //app.loginServiceName = "";
        // 
        var session = window.sessionStorage;
       
        var token =  session.getItem('token');
     
        if (!token && !debug) {
            var url = window.location.protocol + '//' + window.location.host + '/share/preview/' + accesstoken + '.html';
            //console.log(url);
            self.location.href = url;
            return;
        }
        rsd.setAppToken(token);
        var jsHost = document.location.origin || '//agent.redmicro.cn/redjs/';
        if (document.location.host == "localhost") {
            jsHost = document.location.origin + "/redjs/"
            Rsd.__jsHomeHost = jsHost;
            Rsd.__isAgentHost = false;
        }
       
        setTimeout(function(){
             
            //推入一个新的state防止后退
            history.pushState({accessToken:accesstoken}, null, "#reload");
            history.pushState({}, null,  "#reload");
            //拦截后退操作 
            window.addEventListener('popstate', function(e) {
                 
                var url = window.location.origin + '/share/preview/' + accesstoken + '.html';
                alert('popstate:'+url);
                //后退处理
                //if (e.state == null) {
                //   window.location.href = url;
                //}
                // 阻止默认后退行为
                //e.preventDefault();
            });
            
        },1000);
         
        var downloadService = document.getElementById('downloadServiceName').value;
        
        if (rsd.isEmpty(downloadService)) {
            alert('未设置DownloadService属性');
            document.body.appendChild(document.createTextNode('[未设置DownloadService属性]')); 
            return;
        }

        var _fn = function (data) {

            $r.r(downloadService, {accessToken:accesstoken})
                .then(function (resp) {
                    //console.log(resp.data);
                    var container = document.getElementById('container');
                    container.innerHTML = resp.data||'空'; 
                    try
                    {
                        var loading = document.getElementById('loading_container');
                        if(loading)
                        {
                            loading.remove();
                        }  
                    }
                    catch(err)
                    {
                        alert(err.message);
                    }
                     
                })
                .catch(function (resp) { 
                    try
                    {
                        var loading = document.getElementById('loading_container');
                        if(loading)
                        {
                            loading.remove();
                        }  
                    }
                    catch(err)
                    {
                        alert(err.message);
                    }

                    if (resp.code == 'W2110' || resp.code == 'W2112')//身份验证失败
                    { 
                        window.location.href = window.location.origin + '/share/preview/' + accessToken + '.html';
                    }
                    else
                    {
                        alert(resp.msg);
                        console.error(resp);
                    } 
                });
        };
       
        if (rsd.app.containsService(downloadService)) {
            _fn();
        }
        else {
            rsd.app.on('cms-api', _fn); 
        }
    }

})()

